package util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * MAP操作工具类
 *
 * @author yuangz
 */
public class MapUtil {

    /**
     * 获取字符串
     *
     * @param map
     * @param key
     * @return
     */
    public static String getString(Map<String, Object> map, String key) {
        if (map != null && key != null) {
            return map.get(key) == null ? "" : map.get(key).toString().trim();
        }
        return "";
    }

    /**
     * 获取int
     *
     * @param map
     * @param key
     * @return
     */
    public static int getInt(Map<String, Object> map, String key) {
        try {
            //return Integer.parseInt(MoneyUtil.formatStrToNumber(map.get(key).toString().trim(),0));
            return 0;
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 获取int
     *
     * @param map
     * @param key
     * @return
     */
    public static int getInt(Map<String, Object> map, String key, int defaultValue) {
        try {
            //return Integer.parseInt(MoneyUtil.formatStrToNumber(map.get(key).toString().trim(),0));
        } catch (Exception e) {
            return defaultValue;
        }
        return 0;
    }

    public static double getDouble(Map<String, Object> map, String key) {
        try {
            return Double.parseDouble(map.get(key).toString().trim());
        } catch (Exception e) {
            throw e;
        }
    }


    /**
     * 获取Double
     *
     * @param map
     * @param key
     * @return
     */
    public static double getDouble(Map<String, Object> map, String key, Double defaultValue) {
        try {
            return Double.parseDouble(map.get(key).toString().trim());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * 获取long
     *
     * @param map
     * @param key
     * @return
     */
    public static long getLong(Map<String, Object> map, String key) {
        try {
            //return Long.parseLong(MoneyUtil.formatStrToNumber(map.get(key).toString().trim(),0));
        } catch (Exception e) {
            throw e;
        }
        return 0;
    }

    /**
     * 获取long
     *
     * @param map
     * @param key
     * @return
     */
    public static long getLong(Map<String, Object> map, String key, long defaultValue) {
        try {
            //return Long.parseLong(MoneyUtil.formatStrToNumber(map.get(key).toString().trim(),0));
        } catch (Exception e) {
            return defaultValue;
        }
        return 0;
    }

    /**
     * 获取List  可能为空
     *
     * @param map
     * @param key
     * @return
     */
    @SuppressWarnings("unchecked")
    public static List<Map<String, Object>> getList(Map<String, Object> map, String key) {
        if (map != null && key != null) {
            Object object = map.get(key);
            if (object instanceof List) {
                return (List<Map<String, Object>>) object;
            }
        }
        return null;
    }

    /**
     * 获取map 可能为空
     *
     * @param map
     * @param key
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> getMap(Map<String, Object> map, String key) {
        if (map != null && key != null) {
            Object object = map.get(key);
            if (object instanceof Map) {
                return (Map<String, Object>) object;
            }
        }
        return new HashMap<>(0);
    }
}
